<?php
namespace app\common\model;
use think\facade\Request;
use think\facade\Cache;
use think\Db;
use think\facade\Session;
/**
 * 商品SKU模型
 * Class GoodsSku
 * @package app\common\model
 */
class Renovation extends BaseModel
{
    protected $name = 'merchant';
    protected $pk = 'merchant_id';
	
	public function getlist($info, $categoryId = 0, $search = '',$market = '',$goodsId = '', $class = 1, $sortType = 'all', $sortPrice = false, $listRows = 10){
        $filter = [];
        $sort = [];
        $houtai = Session::get('houtaizhuangtai');
        if(!empty($houtai)&&$houtai ==1){
            $filter[] =['merchant_user_id','=',self::$user_id];
        }
        if(!empty($info['name'])){
            $filter[] =['merchant_name','like','%'.$info['name'].'%'];
        }
        if(!empty($info['is_delete'])){
            $filter[] =['is_delete','=',$info['is_delete']];
        }
        if(!empty($categoryId)){
            $filter[] =['state','=',$categoryId];
        }
        $sort = ['create_time' => 'desc'];

        $list = $this
            ->where($filter)
            ->order($sort)
            ->paginate($listRows, false, [
                'query' => Request::instance()->request()
            ]);
        return $list;
	}

    /**
     * 详情
     * @param $where
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function shoudetail($where)
    {
        is_array($where) ? $filter = $where : $filter['merchant_id'] = (int)$where;
        return self::get($filter);
    }

    /**
     * 修改状态
     * @param array $where
     * @return int|string
     */
    public function saveMerchant($merchant_id = '', $type = '1'){
        if(is_array($merchant_id)){
            $goods_id = implode(',',$merchant_id);
        }
        $where[]=['merchant_id','in',$merchant_id];
        return $this->where($where)->update(['is_delete'=>$type]);
    }

    /**
     * 新增用户
     * @param null|static $user
     * @param $data
     * @return false|int
     */
    public function add($user, $data)
    {
        // 添加商户
        $merchant_name = $data['merchant_name'];
        $merchant_set = $data['merchant_set'];
        $shenfz = $data['shenfz'];
        $address = $data['address'];
        $phone = $data['phone'];
        $images1 = $data['images1'];
        $images2 = $data['images2'];
        $images3 = $data['images3'];
        $images4 = $data['images4'];

        // 添加商户
        $info =array();
        if(!empty($data['state'])){
            if($data['state'] == '1' ||$data['state'] == '2' ||$data['state'] == '3' ||$data['state'] == '4'){
                if(data['state'] == '1'){
                    $info['daimabianma'] =$data['daimabianma'];
                }
                $info['merchant_set'] = $data['merchant_set'];
                $info['address'] = $data['address'];
            }elseif ($data['state'] == '5'|| $data['state'] == '6'){

            }
            $info['merchant_name'] =$data['merchant_name'];
            $info['phone'] = $data['phone'];
            $info['shenfz'] = $data['shenfz'];
            $info['images1'] = $data['images1'];
            $info['images2'] = $data['images2'];
            $info['images3'] = $data['images3'];
            $info['images4'] = $data['images4'];
            $info['state'] = $data['state'];
            $info['merchant_user_id'] = $user['merchant_user_id'];

            if($this->save($info)){
                return 1000;
            }else{
                return 1009;
            }
        }
        return 1008;
    }

    /**
     * 修改
     */
    public function updatemen($id,$upda)
    {
        return DB::table('bfb_merchant')->where('merchant_id',$id)->update($upda);
    }

    /**
     * 团队列表
     */
    public function seltuandui($id,$upda = '')
    {
        $where['a.merchant_id']=$id;
        $where['a.code']='1';
        if(!empty($upda)){
            $where['a.types']=$upda;
        }
        return DB::table('bfb_merchant_tuandui')
            ->alias('a')
            ->join('bfb_merchant b','a.merchant_pid = b.merchant_id')
            ->where($where)
            ->order('a.create_time desc')
            ->field('a.*,b.merchant_name,b.logo,b.address')
            ->select();
    }

    /**
     * 工地
     */
    public function gongdilist($id,$upda = '')
    {
        $where['merchant_id']=$id;
        $where['code']='1';
        return DB::table('bfb_merchant_gongdi')
            ->where($where)
            ->order('create_time desc')
            ->select();
    }

    /**
     * 工地
     */
    public function gongdione($id,$upda)
    {
        $where['merchant_id']=$id;
        $where['code']='1';
        $where['merchant_gongdi_id']=$upda;
        return DB::table('bfb_merchant_gongdi')
            ->where($where)
            ->order('create_time desc')
            ->find();
    }


    /**
     * 工地
     */
    public function gongdimerone($id)
    {
        $where['merchant_id']=$id;
        return DB::table('bfb_merchant')
            ->where($where)
            ->field('merchant_name,logo,merchant_id')
            ->order('create_time desc')
            ->find();
    }
    /**
     * 工地
     */
    public function gongdimer($id)
    {
        $asd = explode(',',$id);
        $where['merchant_id']=['IN',$asd];
        return DB::table('bfb_merchant')
            ->where('merchant_id','in',$id)
            ->field('merchant_name,logo,merchant_id')
            ->order('create_time desc')
            ->select();
    }

    /**
     * 工地
     */
    public function pcgongdilist($name = '')
    {
        $houtai = Session::get('houtaizhuangtai');
        if(!empty($houtai)&&$houtai ==1){
            $where['a.merchant_id']=self::$user_id;
        }
        if(!empty($name)){
            $where['a.gongdi_name']=['=',$name];
        }
        $where['a.code']='1';
        return DB::table('bfb_merchant_gongdi')
            ->alias('a')
            ->leftjoin('bfb_merchant b','a.gongdi_zhu = b.merchant_id')
            ->where($where)
            ->field('a.*,b.merchant_name,b.logo')
            ->order('create_time desc')
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
    }

    /**
     * 工地
     */
    public function pcgongdione($id)
    {
        $where['a.code']='1';
        $where['a.merchant_gongdi_id']=$id;
        return DB::table('bfb_merchant_gongdi')
            ->alias('a')
            ->leftjoin('bfb_merchant b','b.merchant_id = a.gongdi_zhu')
            //->rightJoin('bfb_merchant c','c.merchant_id in (a.gongdi_she)')
            //->rightJoin('bfb_merchant d','d.merchant_id in (a.gongdi_gong)')
            ->where($where)
            ->order('a.create_time desc')
            ->find();
    }

    public function pcgongdiadd($data)
    {
        $info['gongdi_name'] =$data['gongdi_name'];
        $info['gongdi_jieshao'] = $data['gongdi_jieshao'];
        if(!empty($data['gongdi_zhu'])){
            $info['gongdi_zhu'] = $data['gongdi_zhu'];
        }
        if(!empty($data['gongdi_she'])){
            $info['gongdi_she'] = implode(',',$data['gongdi_she']);
        }
        if(!empty($data['gongdi_gong'])){
            $info['gongdi_gong'] = implode(',',$data['gongdi_gong']);
        }
        $info['gongdi_log'] = $data['gongdi_log'];
        $info['gongdi_address'] = $data['gongdi_address'];
        if(!empty($data['province_id'])){
            $info['provinceid'] = $data['province_id'];
        }
        if(!empty($data['city_id'])){
            $info['cityid'] = $data['city_id'];
        }
        if(!empty($data['district_id'])){
            $info['districtid'] = $data['district_id'];
        }
        $info['create_time'] = time();
        $info['merchant_id']=self::$user_id;
        return Db::table('bfb_merchant_gongdi')->insert($info);
    }

    public function pcgongdiup($data,$id)
    {
        $info['gongdi_name'] =$data['gongdi_name'];
        $info['gongdi_jieshao'] = $data['gongdi_jieshao'];
        if(!empty($data['gongdi_zhu'])){
            $info['gongdi_zhu'] = $data['gongdi_zhu'];
        }
        if(!empty($data['gongdi_she'])){
            $info['gongdi_she'] = implode(',',$data['gongdi_she']);
        }
        if(!empty($data['gongdi_gong'])){
            $info['gongdi_gong'] = implode(',',$data['gongdi_gong']);
        }
        $info['gongdi_log'] = $data['gongdi_log'];
        $info['gongdi_address'] = $data['gongdi_address'];
        if(!empty($data['province_id'])){
            $info['provinceid'] = $data['province_id'];
        }
        if(!empty($data['city_id'])){
            $info['cityid'] = $data['city_id'];
        }
        if(!empty($data['district_id'])){
            $info['districtid'] = $data['district_id'];
        }
        $info['create_time'] = time();
        $info['merchant_id']=self::$user_id;
        return Db::table('bfb_merchant_gongdi')->where('merchant_gongdi_id',$id)->update($info);
    }
    public function pcgongdidelete($id)
    {
        $info['code'] = 2;
        return Db::table('bfb_merchant_gongdi')->where('merchant_gongdi_id',$id)->update($info);
    }

    /**
     * 团队列表
     */
    public function pcshejis($name,$upda = '1')
    {
        $houtai = Session::get('houtaizhuangtai');
        if(!empty($houtai)&&$houtai ==1){
            $where['a.merchant_id']=self::$user_id;
        }
        $where['a.code']='1';
        if(!empty($upda)){
            $where['a.types']=$upda;
        }
        if(!empty($name)){
            $where['b.merchant_name']=$name;
        }
        return DB::table('bfb_merchant_tuandui')
            ->alias('a')
            ->join('bfb_merchant b','a.merchant_pid = b.merchant_id')
            ->where($where)
            ->order('a.create_time desc')
            ->field('a.*,b.merchant_name,b.logo,b.address')
            ->paginate(15, false, [
                'query' => Request::instance()->request()
            ]);
    }

    public function shejisdelete($id)
    {
        $info['code'] = 2;
        return Db::table('bfb_merchant_tuandui')->where('merchant_tuandui_id',$id)->update($info);
    }

    /*
     * 地区查询
     * */
    public function citylist($where)
    {
        return Db::table('bfb_city')->where($where)->find();
    }

    /*
     * 地区查询
     * */
    public function userOne($where,$state)
    {
        return Db::table('bfb_merchant_user')
            ->alias('a')
            ->join('bfb_merchant b','a.user_id =b.merchant_user_id')
            ->where('b.state','=',$state)
            ->where($where)
            ->find();
    }

    /*
     * 验证码入库
    */
    public function addYanzm($where,$phone){

        $list = DB::table('bfb_merchant_code')->where('phone','=',$phone)->field('create_time')->order('create_time','DESC')->find();
        if(!empty($list)){
            //echo date('Y-m-d H:i:s',$list['create_time']);
            $remain = time()-$list['create_time'];
            $mins = intval($remain/60);
            if($mins<15){
                return 1003;
            }
        }
        if(DB::table('bfb_merchant_code')->insert($where)){
            return 1000;
        }else{
            return 1002;
        }

    }

    /*
	*下级入库
	* @return array
	*/
    public function addTuandui($info,$type = '1'){

        $where['merchant_pid'] = $info['merchant_id'];
        $where['types'] = $type;
        $where['create_time'] = time();
        $where['merchant_id'] = self::$user_id;
        return DB::table('bfb_merchant_tuandui')->insert($where);
    }

    /**
     * 详情
     */
    public static function zhanghaodan($where)
    {
        return DB::table('bfb_merchant')
            ->where($where)
            ->field('merchant_id')
            ->find();
    }


}